
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.validators &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-validators">
            
  <h1>Source code for django.core.validators</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="n">ValidationError</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deconstruct</span> <span class="k">import</span> <span class="n">deconstructible</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">SimpleLazyObject</span>
<span class="kn">from</span> <span class="nn">django.utils.ipv6</span> <span class="k">import</span> <span class="n">is_valid_ipv6_address</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="k">import</span> <span class="n">urlsplit</span><span class="p">,</span> <span class="n">urlunsplit</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">ungettext_lazy</span>

<span class="c1"># These values, if given to validate(), will trigger the self.required check.</span>
<span class="n">EMPTY_VALUES</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="p">[],</span> <span class="p">(),</span> <span class="p">{})</span>


<span class="k">def</span> <span class="nf">_lazy_re_compile</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Lazily compile a regex with flags.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">_compile</span><span class="p">():</span>
        <span class="c1"># Compile the regex if it was not passed pre-compiled.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="ow">not</span> <span class="n">flags</span><span class="p">,</span> <span class="s2">&quot;flags must be empty if regex is passed pre-compiled&quot;</span>
            <span class="k">return</span> <span class="n">regex</span>
    <span class="k">return</span> <span class="n">SimpleLazyObject</span><span class="p">(</span><span class="n">_compile</span><span class="p">)</span>


<div class="viewcode-block" id="RegexValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.RegexValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">RegexValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">regex</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid value.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;invalid&#39;</span>
    <span class="n">inverse_match</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">flags</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">inverse_match</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">regex</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span> <span class="o">=</span> <span class="n">regex</span>
        <span class="k">if</span> <span class="n">message</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="k">if</span> <span class="n">inverse_match</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="o">=</span> <span class="n">inverse_match</span>
        <span class="k">if</span> <span class="n">flags</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">flags</span> <span class="o">=</span> <span class="n">flags</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">flags</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;If the flags are set, regex must be a regular expression string.&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">regex</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">flags</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Validate that the input contains a match for the regular expression</span>
<span class="sd">        if inverse_match is False, otherwise raise ValidationError.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span>
                <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)))):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">RegexValidator</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">flags</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">flags</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">inverse_match</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">inverse_match</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span> <span class="o">==</span> <span class="n">other</span><span class="p">)</span></div>


<div class="viewcode-block" id="URLValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.URLValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">URLValidator</span><span class="p">(</span><span class="n">RegexValidator</span><span class="p">):</span>
    <span class="n">ul</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\u00a1</span><span class="s1">-</span><span class="se">\uffff</span><span class="s1">&#39;</span>  <span class="c1"># unicode letters range (must be a unicode string, not a raw string)</span>

    <span class="c1"># IP patterns</span>
    <span class="n">ipv4_re</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d))</span><span class="si">{3}</span><span class="s1">&#39;</span>
    <span class="n">ipv6_re</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;\[[0-9a-f:\.]+\]&#39;</span>  <span class="c1"># (simple regex, validated later)</span>

    <span class="c1"># Host patterns</span>
    <span class="n">hostname_re</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;[a-z&#39;</span> <span class="o">+</span> <span class="n">ul</span> <span class="o">+</span> <span class="sa">r</span><span class="s1">&#39;0-9](?:[a-z&#39;</span> <span class="o">+</span> <span class="n">ul</span> <span class="o">+</span> <span class="sa">r</span><span class="s1">&#39;0-9-]{0,61}[a-z&#39;</span> <span class="o">+</span> <span class="n">ul</span> <span class="o">+</span> <span class="sa">r</span><span class="s1">&#39;0-9])?&#39;</span>
    <span class="c1"># Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1</span>
    <span class="n">domain_re</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;(?:\.(?!-)[a-z&#39;</span> <span class="o">+</span> <span class="n">ul</span> <span class="o">+</span> <span class="sa">r</span><span class="s1">&#39;0-9-]{1,63}(?&lt;!-))*&#39;</span>
    <span class="n">tld_re</span> <span class="o">=</span> <span class="p">(</span>
        <span class="sa">r</span><span class="s1">&#39;\.&#39;</span>                                <span class="c1"># dot</span>
        <span class="sa">r</span><span class="s1">&#39;(?!-)&#39;</span>                             <span class="c1"># can&#39;t start with a dash</span>
        <span class="sa">r</span><span class="s1">&#39;(?:[a-z&#39;</span> <span class="o">+</span> <span class="n">ul</span> <span class="o">+</span> <span class="s1">&#39;-]{2,63}&#39;</span>         <span class="c1"># domain label</span>
        <span class="sa">r</span><span class="s1">&#39;|xn--[a-z0-9]{1,59})&#39;</span>              <span class="c1"># or punycode label</span>
        <span class="sa">r</span><span class="s1">&#39;(?&lt;!-)&#39;</span>                            <span class="c1"># can&#39;t end with a dash</span>
        <span class="sa">r</span><span class="s1">&#39;\.?&#39;</span>                               <span class="c1"># may have a trailing dot</span>
    <span class="p">)</span>
    <span class="n">host_re</span> <span class="o">=</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="n">hostname_re</span> <span class="o">+</span> <span class="n">domain_re</span> <span class="o">+</span> <span class="n">tld_re</span> <span class="o">+</span> <span class="s1">&#39;|localhost)&#39;</span>

    <span class="n">regex</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span>
        <span class="sa">r</span><span class="s1">&#39;^(?:[a-z0-9\.\-\+]*)://&#39;</span>  <span class="c1"># scheme is validated separately</span>
        <span class="sa">r</span><span class="s1">&#39;(?:\S+(?::\S*)?@)?&#39;</span>  <span class="c1"># user:pass authentication</span>
        <span class="sa">r</span><span class="s1">&#39;(?:&#39;</span> <span class="o">+</span> <span class="n">ipv4_re</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span> <span class="o">+</span> <span class="n">ipv6_re</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span> <span class="o">+</span> <span class="n">host_re</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span>
        <span class="sa">r</span><span class="s1">&#39;(?::\d{2,5})?&#39;</span>  <span class="c1"># port</span>
        <span class="sa">r</span><span class="s1">&#39;(?:[/?#][^\s]*)?&#39;</span>  <span class="c1"># resource path</span>
        <span class="sa">r</span><span class="s1">&#39;\Z&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid URL.&#39;</span><span class="p">)</span>
    <span class="n">schemes</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;http&#39;</span><span class="p">,</span> <span class="s1">&#39;https&#39;</span><span class="p">,</span> <span class="s1">&#39;ftp&#39;</span><span class="p">,</span> <span class="s1">&#39;ftps&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">schemes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">schemes</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">schemes</span> <span class="o">=</span> <span class="n">schemes</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="c1"># Check first if the scheme is valid</span>
        <span class="n">scheme</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;://&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">scheme</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">schemes</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="c1"># Then check full URL</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__call__</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="c1"># Trivial case failed. Try for possible IDN domain</span>
            <span class="k">if</span> <span class="n">value</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>  <span class="c1"># for example, &quot;Invalid IPv6 URL&quot;</span>
                    <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">netloc</span> <span class="o">=</span> <span class="n">netloc</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>  <span class="c1"># IDN -&gt; ACE</span>
                <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>  <span class="c1"># invalid domain part</span>
                    <span class="k">raise</span> <span class="n">e</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">urlunsplit</span><span class="p">((</span><span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span><span class="p">))</span>
                <span class="nb">super</span><span class="p">(</span><span class="n">URLValidator</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__call__</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Now verify IPv6 in the netloc part</span>
            <span class="n">host_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^\[(.+)\](?::\d{2,5})?$&#39;</span><span class="p">,</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">netloc</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">host_match</span><span class="p">:</span>
                <span class="n">potential_ip</span> <span class="o">=</span> <span class="n">host_match</span><span class="o">.</span><span class="n">groups</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">validate_ipv6_address</span><span class="p">(</span><span class="n">potential_ip</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="c1"># The maximum length of a full host name is 253 characters per RFC 1034</span>
        <span class="c1"># section 3.1. It&#39;s defined to be 255 bytes or less, but this includes</span>
        <span class="c1"># one byte for the length of the name and one byte for the trailing dot</span>
        <span class="c1"># that&#39;s used to indicate absolute names in DNS.</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">urlsplit</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">netloc</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">253</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span></div>


<span class="n">integer_validator</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span>
    <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^-?\d+\Z&#39;</span><span class="p">),</span>
    <span class="n">message</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid integer.&#39;</span><span class="p">),</span>
    <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid&#39;</span><span class="p">,</span>
<span class="p">)</span>


<span class="k">def</span> <span class="nf">validate_integer</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">integer_validator</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>


<div class="viewcode-block" id="EmailValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.EmailValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">EmailValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid email address.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;invalid&#39;</span>
    <span class="n">user_regex</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span>
        <span class="sa">r</span><span class="s2">&quot;(^[-!#$%&amp;&#39;*+/=?^_`</span><span class="si">{}</span><span class="s2">|~0-9A-Z]+(\.[-!#$%&amp;&#39;*+/=?^_`</span><span class="si">{}</span><span class="s2">|~0-9A-Z]+)*\Z&quot;</span>  <span class="c1"># dot-atom</span>
        <span class="sa">r</span><span class="s1">&#39;|^&quot;([\001-\010\013\014\016-\037!#-\[\]-\177]|</span><span class="se">\\</span><span class="s1">[\001-\011\013\014\016-\177])*&quot;\Z)&#39;</span><span class="p">,</span>  <span class="c1"># quoted-string</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">domain_regex</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span>
        <span class="c1"># max length for domain name labels is 63 characters per RFC 1034</span>
        <span class="sa">r</span><span class="s1">&#39;((?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+)(?:[A-Z0-9-]{2,63}(?&lt;!-))\Z&#39;</span><span class="p">,</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">literal_regex</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span>
        <span class="c1"># literal form, ipv4 or ipv6 address (SMTP 4.1.3)</span>
        <span class="sa">r</span><span class="s1">&#39;\[([A-f0-9:\.]+)\]\Z&#39;</span><span class="p">,</span>
        <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
    <span class="n">domain_whitelist</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;localhost&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">whitelist</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">message</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="k">if</span> <span class="n">whitelist</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="o">=</span> <span class="n">whitelist</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span> <span class="ow">or</span> <span class="s1">&#39;@&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="n">user_part</span><span class="p">,</span> <span class="n">domain_part</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">user_part</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">domain_part</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">validate_domain_part</span><span class="p">(</span><span class="n">domain_part</span><span class="p">)):</span>
            <span class="c1"># Try for possible IDN domain-part</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">domain_part</span> <span class="o">=</span> <span class="n">domain_part</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;ascii&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">validate_domain_part</span><span class="p">(</span><span class="n">domain_part</span><span class="p">):</span>
                    <span class="k">return</span>
            <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">validate_domain_part</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">domain_part</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">domain_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">domain_part</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>

        <span class="n">literal_match</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">literal_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">domain_part</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">literal_match</span><span class="p">:</span>
            <span class="n">ip_address</span> <span class="o">=</span> <span class="n">literal_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">validate_ipv46_address</span><span class="p">(</span><span class="n">ip_address</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
                <span class="k">pass</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">EmailValidator</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">domain_whitelist</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">domain_whitelist</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="ow">and</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
        <span class="p">)</span></div>


<span class="n">validate_email</span> <span class="o">=</span> <span class="n">EmailValidator</span><span class="p">()</span>

<span class="n">slug_re</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^[-a-zA-Z0-9_]+\Z&#39;</span><span class="p">)</span>
<span class="n">validate_slug</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span>
    <span class="n">slug_re</span><span class="p">,</span>
    <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Enter a valid &#39;slug&#39; consisting of letters, numbers, underscores or hyphens.&quot;</span><span class="p">),</span>
    <span class="s1">&#39;invalid&#39;</span>
<span class="p">)</span>

<span class="n">slug_unicode_re</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^[-\w]+\Z&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">U</span><span class="p">)</span>
<span class="n">validate_unicode_slug</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span>
    <span class="n">slug_unicode_re</span><span class="p">,</span>
    <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Enter a valid &#39;slug&#39; consisting of Unicode letters, numbers, underscores, or hyphens.&quot;</span><span class="p">),</span>
    <span class="s1">&#39;invalid&#39;</span>
<span class="p">)</span>

<span class="n">ipv4_re</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]))</span><span class="si">{3}</span><span class="s1">\Z&#39;</span><span class="p">)</span>
<span class="n">validate_ipv4_address</span> <span class="o">=</span> <span class="n">RegexValidator</span><span class="p">(</span><span class="n">ipv4_re</span><span class="p">,</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv4 address.&#39;</span><span class="p">),</span> <span class="s1">&#39;invalid&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="validate_ipv6_address"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.validate_ipv6_address">[docs]</a><span class="k">def</span> <span class="nf">validate_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">is_valid_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv6 address.&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="validate_ipv46_address"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.validate_ipv46_address">[docs]</a><span class="k">def</span> <span class="nf">validate_ipv46_address</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">validate_ipv4_address</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">validate_ipv6_address</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv4 or IPv6 address.&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid&#39;</span><span class="p">)</span></div>


<span class="n">ip_address_validator_map</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;both&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv46_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv4 or IPv6 address.&#39;</span><span class="p">)),</span>
    <span class="s1">&#39;ipv4&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv4_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv4 address.&#39;</span><span class="p">)),</span>
    <span class="s1">&#39;ipv6&#39;</span><span class="p">:</span> <span class="p">([</span><span class="n">validate_ipv6_address</span><span class="p">],</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter a valid IPv6 address.&#39;</span><span class="p">)),</span>
<span class="p">}</span>


<span class="k">def</span> <span class="nf">ip_address_validators</span><span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="n">unpack_ipv4</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Depending on the given parameters returns the appropriate validators for</span>
<span class="sd">    the GenericIPAddressField.</span>

<span class="sd">    This code is here, because it is exactly the same for the model and the form field.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">protocol</span> <span class="o">!=</span> <span class="s1">&#39;both&#39;</span> <span class="ow">and</span> <span class="n">unpack_ipv4</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;You can only use `unpack_ipv4` if `protocol` is set to &#39;both&#39;&quot;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ip_address_validator_map</span><span class="p">[</span><span class="n">protocol</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The protocol &#39;</span><span class="si">%s</span><span class="s2">&#39; is unknown. Supported: </span><span class="si">%s</span><span class="s2">&quot;</span>
                         <span class="o">%</span> <span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">ip_address_validator_map</span><span class="p">)))</span>


<div class="viewcode-block" id="int_list_validator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.int_list_validator">[docs]</a><span class="k">def</span> <span class="nf">int_list_validator</span><span class="p">(</span><span class="n">sep</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;invalid&#39;</span><span class="p">,</span> <span class="n">allow_negative</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="n">regexp</span> <span class="o">=</span> <span class="n">_lazy_re_compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^</span><span class="si">%(neg)s</span><span class="s1">\d+(?:</span><span class="si">%(sep)s%(neg)s</span><span class="s1">\d+)*\Z&#39;</span> <span class="o">%</span> <span class="p">{</span>
        <span class="s1">&#39;neg&#39;</span><span class="p">:</span> <span class="s1">&#39;(-)?&#39;</span> <span class="k">if</span> <span class="n">allow_negative</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
        <span class="s1">&#39;sep&#39;</span><span class="p">:</span> <span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">sep</span><span class="p">),</span>
    <span class="p">})</span>
    <span class="k">return</span> <span class="n">RegexValidator</span><span class="p">(</span><span class="n">regexp</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="n">code</span><span class="p">)</span></div>


<span class="n">validate_comma_separated_integer_list</span> <span class="o">=</span> <span class="n">int_list_validator</span><span class="p">(</span>
    <span class="n">message</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s1">&#39;Enter only digits separated by commas.&#39;</span><span class="p">),</span>
<span class="p">)</span>


<span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">BaseValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Ensure this value is </span><span class="si">%(limit_value)s</span><span class="s1"> (it is </span><span class="si">%(show_value)s</span><span class="s1">).&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;limit_value&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">limit_value</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span> <span class="o">=</span> <span class="n">limit_value</span>
        <span class="k">if</span> <span class="n">message</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">cleaned</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">clean</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;limit_value&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span><span class="p">,</span> <span class="s1">&#39;show_value&#39;</span><span class="p">:</span> <span class="n">cleaned</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">:</span> <span class="n">value</span><span class="p">}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">cleaned</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">limit_value</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">limit_value</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">a</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">b</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span>


<div class="viewcode-block" id="MaxValueValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MaxValueValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">MaxValueValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Ensure this value is less than or equal to </span><span class="si">%(limit_value)s</span><span class="s1">.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;max_value&#39;</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span></div>


<div class="viewcode-block" id="MinValueValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MinValueValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">MinValueValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s1">&#39;Ensure this value is greater than or equal to </span><span class="si">%(limit_value)s</span><span class="s1">.&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;min_value&#39;</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span></div>


<div class="viewcode-block" id="MinLengthValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MinLengthValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">MinLengthValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
        <span class="s1">&#39;Ensure this value has at least </span><span class="si">%(limit_value)d</span><span class="s1"> character (it has </span><span class="si">%(show_value)d</span><span class="s1">).&#39;</span><span class="p">,</span>
        <span class="s1">&#39;Ensure this value has at least </span><span class="si">%(limit_value)d</span><span class="s1"> characters (it has </span><span class="si">%(show_value)d</span><span class="s1">).&#39;</span><span class="p">,</span>
        <span class="s1">&#39;limit_value&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;min_length&#39;</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></div>


<div class="viewcode-block" id="MaxLengthValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.MaxLengthValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">MaxLengthValidator</span><span class="p">(</span><span class="n">BaseValidator</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
        <span class="s1">&#39;Ensure this value has at most </span><span class="si">%(limit_value)d</span><span class="s1"> character (it has </span><span class="si">%(show_value)d</span><span class="s1">).&#39;</span><span class="p">,</span>
        <span class="s1">&#39;Ensure this value has at most </span><span class="si">%(limit_value)d</span><span class="s1"> characters (it has </span><span class="si">%(show_value)d</span><span class="s1">).&#39;</span><span class="p">,</span>
        <span class="s1">&#39;limit_value&#39;</span><span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;max_length&#39;</span>

    <span class="k">def</span> <span class="nf">compare</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></div>


<div class="viewcode-block" id="DecimalValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.DecimalValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">DecimalValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate that the input does not exceed the maximum number of digits</span>
<span class="sd">    expected, otherwise raise ValidationError.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;max_digits&#39;</span><span class="p">:</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> digit in total.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> digits in total.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;max&#39;</span>
        <span class="p">),</span>
        <span class="s1">&#39;max_decimal_places&#39;</span><span class="p">:</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> decimal place.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> decimal places.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;max&#39;</span>
        <span class="p">),</span>
        <span class="s1">&#39;max_whole_digits&#39;</span><span class="p">:</span> <span class="n">ungettext_lazy</span><span class="p">(</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> digit before the decimal point.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;Ensure that there are no more than </span><span class="si">%(max)s</span><span class="s1"> digits before the decimal point.&#39;</span><span class="p">,</span>
            <span class="s1">&#39;max&#39;</span>
        <span class="p">),</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_digits</span><span class="p">,</span> <span class="n">decimal_places</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="o">=</span> <span class="n">max_digits</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span> <span class="o">=</span> <span class="n">decimal_places</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">digit_tuple</span><span class="p">,</span> <span class="n">exponent</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">as_tuple</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">decimals</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">exponent</span><span class="p">)</span>
        <span class="c1"># digit_tuple doesn&#39;t include any leading zeros.</span>
        <span class="n">digits</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">digit_tuple</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">decimals</span> <span class="o">&gt;</span> <span class="n">digits</span><span class="p">:</span>
            <span class="c1"># We have leading zeros up to or past the decimal point. Count</span>
            <span class="c1"># everything past the decimal point as a digit. We do not count</span>
            <span class="c1"># 0 before the decimal point as a digit since that would mean</span>
            <span class="c1"># we would not allow max_digits = decimal_places.</span>
            <span class="n">digits</span> <span class="o">=</span> <span class="n">decimals</span>
        <span class="n">whole_digits</span> <span class="o">=</span> <span class="n">digits</span> <span class="o">-</span> <span class="n">decimals</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">digits</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="p">[</span><span class="s1">&#39;max_digits&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;max_digits&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;max&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span><span class="p">},</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">decimals</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="p">[</span><span class="s1">&#39;max_decimal_places&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;max_decimal_places&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;max&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span><span class="p">},</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span>
                <span class="n">whole_digits</span> <span class="o">&gt;</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">messages</span><span class="p">[</span><span class="s1">&#39;max_whole_digits&#39;</span><span class="p">],</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;max_whole_digits&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;max&#39;</span><span class="p">:</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span><span class="p">)},</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">max_digits</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">max_digits</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">decimal_places</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">decimal_places</span>
        <span class="p">)</span></div>


<div class="viewcode-block" id="FileExtensionValidator"><a class="viewcode-back" href="../../../ref/validators.html#django.core.validators.FileExtensionValidator">[docs]</a><span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">FileExtensionValidator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span>
        <span class="s2">&quot;File extension &#39;</span><span class="si">%(extension)s</span><span class="s2">&#39; is not allowed. &quot;</span>
        <span class="s2">&quot;Allowed extensions are: &#39;</span><span class="si">%(allowed_extensions)s</span><span class="s2">&#39;.&quot;</span>
    <span class="p">)</span>
    <span class="n">code</span> <span class="o">=</span> <span class="s1">&#39;invalid_extension&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">allowed_extensions</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">code</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">allowed_extensions</span> <span class="o">=</span> <span class="n">allowed_extensions</span>
        <span class="k">if</span> <span class="n">message</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span>
        <span class="k">if</span> <span class="n">code</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">extension</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">name</span><span class="p">)[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">allowed_extensions</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">extension</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">allowed_extensions</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">message</span><span class="p">,</span>
                <span class="n">code</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">code</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span>
                    <span class="s1">&#39;extension&#39;</span><span class="p">:</span> <span class="n">extension</span><span class="p">,</span>
                    <span class="s1">&#39;allowed_extensions&#39;</span><span class="p">:</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">allowed_extensions</span><span class="p">)</span>
                <span class="p">}</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">allowed_extensions</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">allowed_extensions</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">message</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">code</span>
        <span class="p">)</span></div>


<span class="k">def</span> <span class="nf">get_available_image_extensions</span><span class="p">():</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">PIL</span> <span class="k">import</span> <span class="n">Image</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">[]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">Image</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">ext</span><span class="o">.</span><span class="n">lower</span><span class="p">()[</span><span class="mi">1</span><span class="p">:]</span> <span class="k">for</span> <span class="n">ext</span> <span class="ow">in</span> <span class="n">Image</span><span class="o">.</span><span class="n">EXTENSION</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>


<span class="n">validate_image_file_extension</span> <span class="o">=</span> <span class="n">FileExtensionValidator</span><span class="p">(</span>
    <span class="n">allowed_extensions</span><span class="o">=</span><span class="n">get_available_image_extensions</span><span class="p">(),</span>
<span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>